package com.leyou.item.service.impl;

import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.item.mapper.SpecParamMapper;
import com.leyou.item.pojo.SpecParam;
import com.leyou.item.service.SpecParamService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SpecParamServiceImpl implements SpecParamService {

    @Autowired
    private SpecParamMapper specParamMapper;


    /**
     * 根据各种条件查询规格参数
     * @param gid
     * @param cid
     * @param searching
     * @param generic
     * @return
     */
    @Override
    public List<SpecParam> querySpecParam(Long gid,Long cid,Boolean searching,Boolean generic) {
        SpecParam specParam = new SpecParam();

        specParam.setGroupId(gid);
        specParam.setCid(cid);
        specParam.setSearching(searching);
        specParam.setGeneric(generic);

        List<SpecParam> specParamList = specParamMapper.select(specParam);
        if (CollectionUtils.isEmpty(specParamList)) {
            throw new LyException(ExceptionEnum.SPEC_PARAM_NOT_FOUND);
        }
        return specParamList;
    }

    @Override
    public void saveSpecParam(SpecParam specParam) {

        int count = specParamMapper.insert(specParam);

        if (count ==0){
            throw new LyException(ExceptionEnum.INSERT_SPEC_PARAM_ERROR);
        }
    }

    @Override
    public void updateSpecParam(SpecParam specParam) {
        int count = specParamMapper.updateByPrimaryKey(specParam);
        if (count == 0) {
            throw new LyException(ExceptionEnum.UPDATE_SPEC_PARAM_ERROR);
        }
    }

    @Override
    public void deleteSpecParam(Long id) {
        int count = specParamMapper.deleteByPrimaryKey(id);
        if (count == 0){
            throw new LyException(ExceptionEnum.INTERNAL_SERVER_ERROR);
        }
    }

}
